package org.springframework.fusion.service.impl;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.fusion.service.EmailSendService;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailSendServiceImpl implements EmailSendService {

	@Autowired
	private JavaMailSender mailSender;
	@Autowired
	private MailProperties mailProperties;

	/**
	 * 描述: 单个邮件发送
	 * @param email 邮件地址
	 * @param theme 邮件主题
	 * @param content 邮件内容
	 * 创建人: 赵兴炎  
	 * 日期: 2019年8月16日
	 */
	@Override
	public void sendEmail(String email, String theme, String content) {
		SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
		simpleMailMessage.setFrom(mailProperties.getUsername());
		simpleMailMessage.setTo(email);
		simpleMailMessage.setSubject(theme);
		simpleMailMessage.setText(content);
		mailSender.send(simpleMailMessage);
	}
	
	
	/**
	 * 描述: 发送html邮件
	 * @param email 邮箱地址
	 * @param theme 主题
	 * @param content 内容
	 * @param url  地址
	 * 创建人: 赵兴炎  
	 * 日期: 2019年8月21日
	 */
	@Override
	public void sendEmail(String email, String theme, String content,String url) {
		try {
			MimeMessage message = mailSender.createMimeMessage();
			message.setFrom(new InternetAddress(mailProperties.getUsername()));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
			message.setSubject(theme);
			String contentHtml = "<html>" + "<head></head>" + "<body>" + content+"<br/><br/>"
					+ "<a href='"+url+"' target='_blank' "
					+ "style='display:inline-block;width:100px;height:30px;text-align:center;"
					+ "line-height:30px;background-color:#0490d5;color:#fff;text-decoration:none;"
					+ "border-radius:5px;float:left;'>" + "激活邮箱</a>" + "</body>" + "</html>";
			message.setContent(contentHtml, "text/html;charset=UTF-8");
			message.saveChanges();
			mailSender.send(message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
